package com.example.boot.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.common.LogAnno;
import com.example.common.Result;
import com.example.common.enums.LogType;
import com.example.pojo.dto.AccessDTO;
import com.example.pojo.entity.Project;
import com.example.pojo.vo.ProjectVO;
import com.example.service.Impl.EntrustServiceImpl;
import com.example.service.ProjectService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.time.LocalDate;
import java.util.List;
import java.util.Map;

/**
 * TODO
 *
 * @author ding
 * @since 2024/5/31
 */
@RestController
@Slf4j
@Api(tags = "后台-项目接口")
@RequestMapping("/project")
public class ProjectController {

    @Autowired
    private ProjectService projectService;

    @Autowired
    private EntrustServiceImpl entrustServiceImpl;

    /**
     * 根据id获取项目信息
     */
    @ApiOperation("通过项目Id获取项目信息")
    @GetMapping("/selectById/{id}")
//    @PreAuthorize("hasAuthority('PROJECT_QUERY')")
    public Result selectById(@ApiParam("项目id") @PathVariable Integer id){
        Project project = projectService.selectById(id);
        return Result.success(project);
    }

    /**
     * 分页查询
     */
    @GetMapping("/selectPage")
    @ApiOperation("根据项目名称，模糊分页查询项目信息")
//    @PreAuthorize("hasAuthority('PROJECTGROUP_QUERY') or authentication.principal.username=='admin'")
    public Result selectPage(@ApiParam("项目名称") String name,
                             @ApiParam("发布单位id") Integer entrustId,
                             @ApiParam("团队id") Integer teamId,
                             @ApiParam("是否为接取项目页面") @RequestParam(required = false) Boolean access,
                             @DateTimeFormat(pattern = "yyyy-MM-dd") @ApiParam("开始工期") LocalDate begin,
                             @DateTimeFormat(pattern = "yyyy-MM-dd") @ApiParam("结束工期") LocalDate end,
                             @ApiParam("前端过滤器传递字段") String statuses,
                             @ApiParam("当前分页数") @RequestParam(defaultValue = "1") Integer pageNum,
                             @ApiParam("当前页面大小") @RequestParam(defaultValue = "10") Integer pageSize) {
        Page<Project> page = projectService.selectPage(name, entrustId, teamId, access, begin, end, statuses, pageNum, pageSize);
        return Result.success(page);
    }

    @LogAnno(title = "项目管理", type = LogType.ADD)
    @ApiOperation(("新增项目信息"))
    @PostMapping("/add")
//    @PreAuthorize("hasAuthority('PROJECT_ADD')")
    public Result add(@ApiParam("项目信息") @RequestBody Project project){
        projectService.addProject(project);
        return Result.success();
    }

    @LogAnno(title = "项目管理", type = LogType.UPDATE)
    @ApiOperation("更新项目信息")
    @PutMapping("/update")
//    @PreAuthorize("hasAuthority('PROJECT_EDIT')")
    public Result update(@ApiParam("项目信息") @RequestBody Project project){
        projectService.updateProject(project);
        return Result.success();
    }

    @ApiOperation("根据id删除项目信息")
    @LogAnno(title = "项目管理", type = LogType.DELETE)
//    @PreAuthorize("hasAuthority('PROJECT_DELETE')")
    @DeleteMapping("/delete/{id}")
    public Result delete(@ApiParam("项目ID") @PathVariable Integer id){
        projectService.deleteById(id);
        return Result.success();
    }

    @ApiOperation("根据id批量删除项目信息")
    @LogAnno(title = "项目管理", type = LogType.DELBATCH)
//    @PreAuthorize("hasAuthority('PROJECT_DELETE')")
    @DeleteMapping("/delete/batch")
    public Result delBatch(@ApiParam("项目ID集合") @RequestBody List<Integer> ids){
        projectService.delBatch(ids);
        return Result.success();
    }

    @ApiOperation("根据用户ID获取其所属团队负责项目信息")
//    @PreAuthorize("hasAuthority('PROJECT_QUERY')")
    @GetMapping("/selectByUserId")
    public Result getProjectListByUserId(){
        List<ProjectVO> list = projectService.getProjectListByUserId();
        return Result.success(list);
    }

    @ApiOperation("施工队接取项目id")
//    @PreAuthorize("hasAuthority('PROJECT_ACCESS')")
    @PutMapping("/access")
    public Result accessProject(@ApiParam("合同相关信息") @RequestBody AccessDTO accessDTO) throws IOException {
        log.info("合同的相关信息为{}", accessDTO);
        projectService.accessProject(accessDTO);
        return Result.success();
    }

    @ApiOperation("获取指定月份中的项目起始时间与截止时间")
//    @PreAuthorize("hasAuthority('PROJECT_QUERY')")
    @GetMapping("/findDataByMonth/{currentYear}/{currentMonth}")
    public Result getProjectInfoByDate(@PathVariable Integer currentYear, @PathVariable Integer currentMonth){
        List<Map<String, Object>> projectInfoByDate = projectService.getProjectInfoByDate(currentYear, currentMonth);
        return Result.success(projectInfoByDate);
    }

    @ApiOperation("获取预算分配")
    @PutMapping("/allotBudget")
    public Result allotBudget(Double budget){
        projectService.allotBudget(budget);
        return Result.success();
    }
}
